/*
 * Copyright 2003-2005 (c) Live Media Pty Ltd. <argot@einet.com.au> 
 *
 * This software is licensed under the Argot Public License 
 * which may be found in the file LICENSE distributed 
 * with this software.
 *
 * More information about this license can be found at
 * http://www.einet.com.au/License
 * 
 * The Developer of this software is Live Media Pty Ltd,
 * PO Box 4591, Melbourne 3001, Australia.  The license is subject 
 * to the law of Victoria, Australia, and subject to exclusive 
 * jurisdiction of the Victorian courts.
 */

using System;
using TypeElement = argot.TypeElement;
using TypeException = argot.TypeException;
using TypeInputStream = argot.TypeInputStream;
using TypeOutputStream = argot.TypeOutputStream;
using TypeReader = argot.TypeReader;
using TypeReaderAuto = argot.TypeReaderAuto;
using TypeLibrary = argot.TypeLibrary;
using TypeWriter = argot.TypeWriter;

namespace argot.meta
{
	
	public class MetaArray
	:MetaBase, TypeReader, TypeWriter, MetaExpression
	{
		public const System.String TYPENAME = "meta.array";
		
		private MetaExpression _size;
		private MetaExpression _type;
		
		public MetaArray(MetaExpression size, MetaExpression type)
		{
			_size = size;
			_type = type;
		}
		
		override public System.String TypeName
		{
			get
			{
				return TYPENAME;
			}
			
		}

		virtual public MetaExpression SizeExpression
		{
			get
			{
				return _size;
			}
			
		}

		virtual public MetaExpression TypeExpression
		{
			get
			{
				return _type;
			}
			
		}

		public override void  Bind(TypeLibrary library, TypeElement definition, System.String typeName, int typeId)
		{
			base.Bind(library, definition, typeName, typeId);
			_size.Bind(library, definition, typeName, typeId);
			_type.Bind(library, definition, typeName, typeId);
		}
		
		public virtual int GetExpressionType(TypeLibrary library)
		{
			return library.GetId(TYPENAME);
		}
		
		public virtual System.Object Read(TypeInputStream inStream, TypeElement element)
		{
			TypeReaderAuto r = new TypeReaderAuto(this.GetType());
			return r.Read(inStream, element);
		}
		
		public virtual void Write(TypeOutputStream outStream, System.Object o, TypeElement element)
		{
			MetaArray ma = (MetaArray) o;
			outStream.WriteObject("meta.expression", ma._size);
			outStream.WriteObject("meta.expression", ma._type);
		}
		
		public virtual System.Object DoRead(TypeInputStream inStream)
		{		
			System.Object sizeObject =  _size.DoRead(inStream);
			
			uint size = 0;
			
			if (sizeObject is System.Byte)
			{
				size = (uint) ((System.Byte) sizeObject);
			}
			else if (sizeObject is System.UInt16)
			{
				size = (uint) ((System.UInt16) sizeObject);
			}
			else if (sizeObject is System.UInt32)
			{
				size = ((System.UInt32) sizeObject);
			}
			else if (sizeObject is System.UInt64)
			{
				size = (uint) ((System.UInt64) sizeObject);
			}
			else
			{
				throw new TypeException("MetaArray not able to use size object");
			}
			
			System.Object[] objects = new System.Object[size];
			for (int x = 0; x < size; x++)
			{
				objects[x] = _type.DoRead(inStream);
			}
			
			return new System.Collections.ArrayList(objects);
		}
	
		public virtual void  DoWrite(TypeOutputStream outStream, System.Object o)
		{
			throw new TypeException( "not implemented" );
		}
	}
}